﻿using System.Collections;
using System.Collections.Generic;
using JSG.BubbleShoot.ScriptableObjects;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
namespace JSG.BubbleShoot
{
    public class Btn_Level : MonoBehaviour
    {
        [HideInInspector]
        public int LevelNum;

        public Text LevelNumText;
        public Image BodyImage;
        public Image LockImage;
        public Image PassedImage;
        public Image StarsImage;

        public Sprite[] BtnBackImages;

        [HideInInspector]
        public bool LevelUnlocked = false;

        [SerializeField]
        private Content m_Contents;
        [SerializeField]
        public DataStorage m_DataStorage;
        // Start is called before the first frame update
        void Start()
        {
            Button b = GetComponent<Button>();
            b.onClick.AddListener(BtnClicked);

            LevelNumText.text = (LevelNum + 1).ToString();


            LevelUnlocked = (LevelNum <= m_DataStorage.LevelUnlocked);



            if (LevelUnlocked)
            {
                GetComponent<Image>().sprite = BtnBackImages[0];
                //LockImage.gameObject.SetActive(false);

                if (LevelNum < m_DataStorage.LevelUnlocked)
                    PassedImage.gameObject.SetActive(true);
                else
                    PassedImage.gameObject.SetActive(false);
                //if (LevelsPassed[LevelNum])
                //{
                //    PassedImage.gameObject.SetActive(true);
                //    //StarsImage.gameObject.SetActive(true);
                //}
                //else
                //{
                //    PassedImage.gameObject.SetActive(false);
                //    //StarsImage.gameObject.SetActive(false);
                //}
            }
            else
            {
                GetComponent<Image>().sprite = BtnBackImages[1];

                //LockImage.gameObject.SetActive(true);
                PassedImage.gameObject.SetActive(false);
                LevelNumText.gameObject.SetActive(false);
                //StarsImage.gameObject.SetActive(false);
            }



        }

        // Update is called once per frame
        void Update()
        {

        }

        public void BtnClicked()
        {
            if (LevelUnlocked)
            {
                //AppMetricaLogingEvents.m_Current.SendLevelStart(LevelNum + 1);

                m_DataStorage.Level = LevelNum;
                m_DataStorage.SaveData();

                SceneManager.LoadScene("Levels");

            }

        }
    }
}